'use strict';

angular.module('core')

.directive('accordionItem', ['$animate', function($animate) {
    return {
        restrict: 'E',
        replace : true,
        transclude : true,
        template : '<div class="accordion-item" ng-transclude></div>',
        require: ['^accordion'],
        link: function (scope, element, attrs, controllers) {
            var accordionCtrl = controllers[0];
            accordionCtrl.registerTarget(element);

            accordionCtrl.registerViewChangeListeners(function () {
                render();
            });

            render();

            function render () {
                var index = accordionCtrl.getTargetIndex(element);
                var active = accordionCtrl.activeIndexes();
                var action = 'removeClass';
                if (angular.isArray(active)) {
                    if (active.indexOf(index) !== -1) {
                        action = 'addClass';
                    }
                } else if (index === active) {
                    action = 'addClass';
                }
                element[action]('in');
            }
        }
    };
}]);